package logic

import (
	"context"
	"fmt"
	"muke-chain/models/mysql"

	"muke-chain/mukerpc/internal/svc"
	"muke-chain/mukerpc/mukerpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddProductLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddProductLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddProductLogic {
	return &AddProductLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddProductLogic) AddProduct(in *mukerpc.AddProductRequest) (*mukerpc.AddProductResponse, error) {
	// todo: add your logic here and delete this line

	product := mysql.Products{
		Name:   in.Name,
		Image:  in.Image,
		Number: in.Number,
		Price:  float64(in.Price),
		Owned:  in.Owned,
		UserId: int(in.UserId),
	}
	err := mysql.DB.Debug().Create(&product).Error
	if err != nil {
		return nil, fmt.Errorf("商品发布失败")
	}

	return &mukerpc.AddProductResponse{Id: int64(product.ID)}, nil
}
